package by.home.actions;


import by.home.*;
import by.home.controller.Params;

import java.io.IOException;
import java.util.Map;

public class ShowChatRoomWithMessages extends ChatAction {
  @Override
  public Params executeAndGet(Map params) throws IOException, ClassNotFoundException {

    int chatRoomId = Integer.valueOf(getStringParameter(params.get("id")));
    String responsePage;
    ChatServiceInterface chatService = this.getChatService();
    ResponseURIGenerator uriGenerator = this.getUriGenerator();

    Parameters parameters = new Parameters();

    ChatRoom chatRoom = chatService.getChatRoomWithMessages(chatRoomId);

    if (chatRoom != null) {
      parameters.setChatRoom(chatRoom);
      responsePage = uriGenerator.generateResponsePageAfterSuccessfullGettingOneChatRoom(chatRoom.getId());
      parameters.setResponsePage(responsePage);
    } else {
      responsePage = uriGenerator.generateResponsePageAfterFailedGettingOneChatRoom();
      parameters.setResponsePage(responsePage);
    }


    return parameters;
  }
}
